<template>
    <div class="survey-container">
        <h1>用户app体验调查</h1>
        <p style="margin-top: 20px; margin-left: 10px;">感谢您参与我们的问卷调查，您的意见对我们非常重要！</p>

        <form @submit.prevent="submitSurvey" style="margin-top: 10px;">
            <div class="question" v-for="(q, index) in question" :key="index">
                <h3>{{ index + 1 }}.{{ q.qname }}</h3>满意度(1-10)
                <div v-if="q.type == 1" class="rating-input-container">
                    <el-input v-model="q.result" type="number" :min="1" :max="10" placeholder="请输入1-10分"
                        class="rating-input">
                        <template #append>分</template>
                    </el-input>
                </div>
                <div v-else class="text-input-container">
                    <el-input v-model="q.result" type="textarea" :rows="3" :placeholder="'请输入您的' + q.qname"
                        class="text-input" resize="none"></el-input>
                </div>
            </div>

            <button type="submit" class="submit-btn" @click="submit">提交问卷</button>
        </form>


    </div>
</template>

<script>
import { toRefs, toRef, reactive } from 'vue';
import axios from '../axios';
import { useRouter, useRoute } from 'vue-router';
import { getSessionStorage } from '../common';
import { ElMessage } from 'element-plus';

export default {
    setup() {
        const router = useRouter()

        const state = reactive({
            question: [],
            userId:getSessionStorage("users").userId
        })

        loadQuestion()
        function loadQuestion() {
            axios.post('/api/queryquestion')
                .then(function (response) {
                    console.log(response)
                    state.question = response.data.data
                    for (let i = 0; i < state.question.length; i++) {
                        state.question[i].result = ""
                        state.question[i].userId = state.userId
                    }
                })
                .catch(function (error) {
                    console.log(error)
                })
        }
        
        function submit(){
            axios.post('/api/insertanswer',state.question)
                .then(function (response) {
                    console.log(response)
                    if(response.data.status == 200){
                        ElMessage.success("提交成功！")
                        router.push("QuestionaireSuccess")
                    }
                })
                .catch(function (error) {
                    ElMessage.error("提交失败！")
                    console.log(error)
                })
        }

        return {
            ...toRefs(state),
            loadQuestion,
            submit
        }

    }
}

</script>

<style scoped>
.survey-container {
    max-width: 800px;
    margin: 0 auto;
    padding: 20px;
    font-family: Arial, sans-serif;
    background-color: #f9f9f9;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

h1 {
    color: #2c3e50;
    text-align: center;
}

.question {
    margin-bottom: 30px;
    padding: 15px;
    background-color: white;
    border-radius: 5px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.options {
    display: flex;
    flex-wrap: wrap;
    gap: 15px;
    margin-top: 10px;
}

.options label {
    display: flex;
    align-items: center;
    cursor: pointer;
}

.options input[type="radio"],
.options input[type="checkbox"] {
    margin-right: 8px;
}

.scale-labels {
    display: flex;
    justify-content: space-between;
    margin-top: 5px;
    font-size: 0.9em;
    color: #666;
}

textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    resize: vertical;
}

.submit-btn {
    display: block;
    width: 100%;
    padding: 12px;
    background-color: #42b983;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 16px;
    cursor: pointer;
    transition: background-color 0.3s;
}

.submit-btn:hover {
    background-color: #3aa876;
}

.thank-you-message {
    text-align: center;
    padding: 30px;
    background-color: white;
    border-radius: 5px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    margin-top: 20px;
}

.thank-you-message h2 {
    color: #42b983;
}

/* 新增的输入框样式 */
.rating-input-container,
.text-input-container {
    margin-top: 12px;
}

/* 评分输入框样式 */
.rating-input {
    width: 220px;
}


.rating-input :deep(.el-input__inner:hover) {
    border-color: #c0c4cc;
}

.rating-input :deep(.el-input__inner:focus) {
    border-color: #409eff;
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.rating-input :deep(.el-input-group__append) {
    background-color: #f5f7fa;
    color: #606266;
    border-left: none;
    padding: 0 15px;
    border-radius: 0 8px 8px 0;
}

/* 文本输入框样式 */
.text-input {
    width: 100%;
}

.text-input :deep(.el-textarea__inner) {
    font-size: 14px;
    padding: 12px 15px;
    border-radius: 8px;
    border: 1px solid #dcdfe6;
    transition: all 0.3s ease;
    box-sizing: border-box;
}

.text-input :deep(.el-textarea__inner:hover) {
    border-color: #c0c4cc;
}

.text-input :deep(.el-textarea__inner:focus) {
    border-color: #409eff;
    box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
    outline: none;
}
</style>